TeeChartPHP
[ class tree: TeeChartPHP ] [ index: TeeChartPHP ] [ all elements ]

Source for file Utils.php

Documentation is available at Utils.php

  1. <?php
  2.  
  3. /**
  4.  * Utils class
  5.  *
  6.  * Description: Chart utility procedures
  7.  *
  8.  * @author
  9.  * @copyright (c) 1995-2008 by Steema Software SL. All Rights Reserved. <info@steema.com>
  10.  * @version 1.0
  11.  * @package TeeChartPHP
  12.  * @subpackage misc
  13.  * @link http://www.steema.com
  14.  */
  15.  
  16.  class Utils {
  17.  
  18.     private $TicksPerMillisecond = 10000;
  19.     private $TicksPerSecond;
  20.     private $TicksPerMinute;
  21.     private $TicksPerHour;
  22.     private $TicksPerDay;
  23.  
  24.     // Number of milliseconds per time unit
  25.     private $MillisPerSecond = 1000;
  26.     private $MillisPerMinute;
  27.     private $MillisPerHour;
  28.     private $MillisPerDay;
  29.  
  30.     private $DaysPerYear = 365;
  31.     // Number of days in 4 years
  32.     private $DaysPer4Years;
  33.     // Number of days in 100 years
  34.     private $DaysPer100Years;
  35.     // Number of days in 400 years
  36.     private $DaysPer400Years;
  37.  
  38.     // Number of days from 1/1/0001 to 12/31/1600
  39.     //static final int DaysTo1601 = DaysPer400Years * 4;
  40.  
  41.     // Number of days from 1/1/0001 to 12/30/1899
  42.     private $DaysTo1899;
  43.     private $DoubleDateOffset;
  44.     // The minimum OA date is 0100/01/01 (Note it's year 100).
  45.     // The maximum OA date is 9999/12/31
  46.     private $OADateMinAsTicks;
  47.  
  48.  
  49.     // Converts RGB color to Hex
  50.     static function rgbhex($red,$green,$blue) {
  51.       $red = dechex($red);
  52.       $green = dechex($green);
  53.       $blue = dechex($blue);
  54.  
  55.       return "#".strtoupper($red.$green.$blue);
  56.     }
  57.  
  58.     // Hex to RGB color
  59.     static function hex2rgb($color) {
  60.         $color = str_replace('#','',$color);
  61.         $s = strlen($color) / 3;
  62.         $r=hexdec(str_repeat(substr($color,0,$s),2/$s));
  63.         $g=hexdec(str_repeat(substr($color,$s,$s),2/$s));
  64.         $b=hexdec(str_repeat(substr($color,2*$s,$s),2/$s));
  65.  
  66.         return new Color($r,$g,$b);
  67.     }
  68.  
  69.         /**
  70.           * Evaluates and returns a steema.<!-- -->teechart.<!-- -->DateTimeStep
  71.           * value as an Axis double scale that may be used to set the
  72.           * steema.<!-- -->teechart.<!-- -->Axis.<!-- -->Increment.
  73.           *
  74.           *
  75.           * @param value DateTimeStep
  76.           * @return double 
  77.           */
  78.  
  79.     public function getDateTimeStep($value) {
  80.         $tmpDateTimeStep = new DateTimeStep();
  81.         return (int) $tmpDateTimeStep->STEP[$value];
  82.     }
  83.  
  84.     public function Utils() {
  85.         $this->TicksPerSecond = $this->TicksPerMillisecond * 1000;
  86.         $this->TicksPerMinute = $this->TicksPerSecond * 60;
  87.         $this->TicksPerHour = $this->TicksPerMinute * 60;
  88.         $this->TicksPerDay = $this->TicksPerHour * 24;
  89.         $this->MillisPerMinute = $this->MillisPerSecond * 60;
  90.         $this->MillisPerHour = $this->MillisPerMinute * 60;
  91.         $this->MillisPerDay = $this->MillisPerHour * 24;
  92.         $this->DaysPer4Years = $this->DaysPerYear * 4 + 1;
  93.         $this->DaysPer100Years = $this->DaysPer4Years * 25 - 1;
  94.         $this->DaysPer400Years = $this->DaysPer100Years * 4 + 1;
  95.         $this->DaysTo1899 = $this->DaysPer400Years * 4 + $this->DaysPer100Years * 3 - 367;
  96.         $this->DoubleDateOffset = $this->DaysTo1899 * $this->TicksPerDay;
  97.         $this->OADateMinAsTicks = ($this->DaysPer100Years - $this->DaysPerYear) * $this->TicksPerDay;
  98.     }
  99.  
  100.  
  101.     /**
  102.      * Recursively delete a directory
  103.      *
  104.      * @param string $dir Directory name
  105.      * @param boolean $deleteRootToo Delete specified top-level directory as well
  106.      */
  107.     static function unlinkRecursive($dir, $deleteRootToo)
  108. {
  109.       if(!$dh = @opendir($dir))
  110.       {
  111.           return;
  112.       }
  113.       while (false !== ($obj = readdir($dh)))
  114.       {
  115.         if($obj == '.' || $obj == '..')
  116.         {
  117.             continue;
  118.         }
  119.  
  120.         if (!@unlink($dir . '/' . $obj))
  121.         {
  122.             unlinkRecursive($dir.'/'.$obj, true);
  123.         }
  124.       }
  125.  
  126.       closedir($dh);
  127.    
  128.       if ($deleteRootToo)
  129.       {
  130.         @rmdir($dir);
  131.       }
  132.    
  133.       return;
  134.     } 
  135.     
  136.     // Copied from Microsoft's SSCLI sources.
  137.     static private function ticksToOADate($value) /* todo throws Exception*/ {
  138.         if ($value == 0) {
  139.             return 0.0; // Returns OleAut's zero'ed date value.
  140.         }
  141.  
  142.         if ($value < $this->TicksPerDay) { // This is a fix for VB. They want the default day to be 1/1/0001 rathar then 12/30/1899.
  143.             $value += $this->DoubleDateOffset; // We could have moved this fix down but we would like to keep the bounds check.
  144.         }
  145.         if ($value < $this->OADateMinAsTicks) {
  146.             throw new Exception("Arg_OleAutDateInvalid");
  147.         }
  148.  
  149.         // Currently, our max date == OA's max date (12/31/9999), so we don't
  150.         // need an overflow check in that direction.
  151.         $millis = ($value - $this->DoubleDateOffset) / $this->TicksPerMillisecond;
  152.         if ($millis < 0) {
  153.             $frac = $millis % $this->MillisPerDay;
  154.             if ($frac != 0) {
  155.                 $millis -= ($this->MillisPerDay + $frac) * 2;
  156.             }
  157.         }
  158.         return (double) $millis / $this->MillisPerDay;
  159.     }
  160.  
  161.     // return double
  162.     static public function stringToDouble($text, $value) {
  163.         if (strlen($text) == 0) {
  164.             return $value;
  165.         } else {
  166.             try {
  167.                 return (double)$text;
  168.             } catch (NumberFormatException $e) {
  169.                 return $value;
  170. //            } catch (FormatException e) {
  171. //                return value;
  172. //            } catch (OverflowException e) {
  173. //                return value;
  174.             }
  175.         }
  176.     }
  177.  
  178.     static public function swapInteger($a, $b)
  179.     {
  180.        $tmpA = round($b);
  181.        $b = $a;
  182.        $a = $tmpA;
  183.     }
  184.  
  185.     static private function privateSort($l, $r, $c, $s) {
  186.         $i = round($l);
  187.         $j = round($r);
  188.         $x = round(($i + $j) / 2);
  189.         while ($i < $j) {
  190.             while ($c->compare($i, $x) < 0) {
  191.                 $i++;
  192.             } while ($c->compare($x, $j) < 0) {
  193.                 $j--;
  194.             }
  195.             if ($i < $j) {
  196.                 $s->swap($i, $j);
  197.                 if ($i == $x) {
  198.                     $x = $j;
  199.                 } else if ($j == $x) {
  200.                     $x = $i;
  201.                 }
  202.             }
  203.             if ($i <= $j) {
  204.                 $i++;
  205.                 $j--;
  206.             }
  207.         }
  208.         if ($l < $j) {
  209.             $this->privateSort($l, $j, $c, $s);
  210.         }
  211.         if ($i < $r) {
  212.             $this->privateSort($i, $r, $c, $s);
  213.         }
  214.     }
  215.  
  216.     static public function sort($startIndex, $endIndex, $c, $s) {
  217.         self::privateSort($startIndex, $endIndex, $c, $s);
  218.     }
  219.  
  220. /* TODO    static public void sort(final int[] x, int startIndex, int endIndex,
  221.                             Comparator c) {
  222.         sort(startIndex, endIndex, c, new Swapper() {
  223.             public void swap(int a, int b) {
  224.                 int tmp = x[a];
  225.                 x[a] = x[b];
  226.                 x[b] = tmp;
  227.             };
  228.         });
  229.     }
  230. */
  231.  
  232.     /**
  233.      * Make a recursive copy of an array
  234.      *
  235.      * @param array $aSource 
  236.      * @return array    copy of source array
  237.      */
  238.     static function  array_copy ($aSource) {
  239.         // check if input is really an array
  240.         if (!is_array($aSource)) {
  241.             throw new Exception("Input is not an Array");
  242.         }
  243.        
  244.         // initialize return array
  245.         $aRetAr = array();
  246.        
  247.         // get array keys
  248.         $aKeys = array_keys($aSource);
  249.         // get array values
  250.         $aVals = array_values($aSource);
  251.        
  252.         // loop through array and assign keys+values to new return array
  253.         for ($x=0;$x<count($aKeys);$x++) {
  254.             // clone if object
  255.             if (is_object($aVals[$x])) {
  256.                 $aRetAr[$aKeys[$x]]=clone $aVals[$x];
  257.             // recursively add array
  258.             } elseif (is_array($aVals[$x])) {
  259.                 $aRetAr[$aKeys[$x]]=array_copy ($aVals[$x]);
  260.             // assign just a plain scalar value
  261.             } else {
  262.                 $aRetAr[$aKeys[$x]]=$aVals[$x];
  263.             }
  264.         }
  265.        
  266.         return $aRetAr;
  267.     }
  268.  
  269.     /**
  270.      * Number of chart Tool types.
  271.      */
  272.     public static $TOOLTYPESCOUNT = 19; //CDI ExtraLegend + GridBand (+2)
  273.  
  274.     /**
  275.      * List of chart Tool classes.
  276.      */
  277. /* TODO    final static public Class[] toolTypesOf = {
  278.                                               Annotation.class,
  279.                                               Rotate.class,
  280.                                               ChartImage.class,
  281.                                               CursorTool.class,
  282.                                               DragMarks.class,
  283.                                               AxisArrow.class,
  284.                                               ColorLine.class,
  285.                                               ColorBand.class,
  286.                                               DrawLine.class,
  287.                                               NearestPoint.class,
  288.                                               MarksTip.class,
  289.                                               ExtraLegend.class,
  290.                                               DragPoint.class,
  291.                                               SeriesAnimation.class,
  292.                                               PieTool.class,
  293.                                               GridTranspose.class,
  294.                                               GanttTool.class,
  295.                                               GridBand.class,
  296. //                                        AxisScrollEditor.class,
  297. //                                        AxisToolEdit.class,
  298. //                                        HotSpotEditor.class,
  299. //                                        LightToolEditor.class,
  300. //                                        ScrollBarEditor.class,
  301. //                                        SurfaceNearestToolEditor.class,
  302. //                                        ToolSeriesEditor.class,
  303. //                                        ZoomToolEditor.class
  304.                                               PageNumber.class
  305.     };
  306. */
  307.  
  308.     /**
  309.      * Returns the index in ToolTypesOf list of a given tool instance.
  310.      *
  311.      * @param tool Tool to search its type in list
  312.      * @return int 
  313.      */
  314. /* tODO    static public int toolTypeIndex(Tool tool) {
  315.         for (int t = 0; t < Utils.TOOLTYPESCOUNT; t++) {
  316.             if (Utils.toolTypesOf[t] == tool.getClass()) {
  317.                 return t;
  318.             }
  319.         }
  320.         return -1;
  321.     }
  322. */
  323.     /**
  324.      * Number of chart Function types.
  325.      */
  326.     public static $FUNCTIONTYPESCOUNT = 37;
  327.  
  328.     /**
  329.      * List of chart Function types.
  330.      */
  331. /* tODO    final static public Class[] functionTypesOf = {
  332.                                                   Add.class, //0
  333.                                                   Subtract.class, //0
  334.                                                   Multiply.class, //0
  335.                                                   Divide.class, //0
  336.                                                   High.class, //0
  337.                                                   Low.class, //0
  338.                                                   Average.class, //0
  339.                                                   Count.class, //0
  340.                                                   Momentum.class,  //2
  341.                                                   MomentumDivision.class,  //2
  342.                                                   Cumulative.class,  //1
  343.                                                   ExpAverage.class,  //1
  344.                                                   Smoothing.class,  //3
  345.                                                   Custom.class,  //3
  346.                                                   RootMeanSquare.class,  //1
  347.                                                   StdDeviation.class,  //1
  348.                                                   Stochastic.class,  //2
  349.                                                   ExpMovAverage.class,  //2
  350.                                                   Performance.class,  //2
  351.                                                   CrossPoints.class,  //3
  352.                                                   CompressOHLC.class,  //2
  353.                                                   CLV.class,  //2
  354.                                                   OBV.class,  //2
  355.                                                   CCI.class,  //2
  356.                                                   MovingAverage.class,  //1
  357.                                                   PVO.class,  //2
  358.                                                   DownSampling.class,  //3
  359.                                                   Trend.class,  //3
  360.                                                   Correlation.class,  //1
  361.                                                   Variance.class,  //1
  362.                                                   Perimeter.class,  //3
  363.                                                   CurveFitting.class,  //3
  364.                                                   ADX.class,  //2
  365.                                                   Bollinger.class,  //2
  366.                                                   MACD.class,  //2
  367.                                                   SAR.class,  //2
  368.                                                   RSI.class  //2
  369.     };
  370.  
  371.     final static public int[] functionGalleryPage = {
  372.             0, 0, 0, 0, 0, 0, 0, 0, 2, 2, 1,
  373.             1, 3, 3, 1, 1, 2, 2, 2, 3, 2, 2,
  374.             2,
  375.             2,
  376.             1, 2, 3, 3, 1,
  377.             1, 3, 3 , 2, 2, 2, 2, 2
  378.     };
  379.  
  380.     static public int seriesTypesIndex(ISeries s) {
  381.         return seriesTypesIndex(s.getClass());
  382.     }
  383.  
  384.     static public int seriesTypesIndex(Class seriesType) {
  385.         for (int t = 0; t < SERIESTYPESCOUNT; t++) {
  386.             if (seriesTypesOf[t] == seriesType) {
  387.                 return t;
  388.             }
  389.         }
  390.         return -1;
  391.     }
  392. */
  393.     public static $SERIESTYPESCOUNT = 46;
  394. /* tODO
  395.     final static public int[] seriesGalleryCount = {
  396.             2, 2, 2, 2, 2, 2, 2, 1, 3
  397.             , 1, 1, 1,
  398.             1, 1, 1, 1, 1, 2, 2, 1, 1, 1, 1, 1,
  399.             1, 1, 1, 1, 1, 1, 1, 2, 2, 1, 1, 1,
  400.             1, 1, 1, 2, 1, 1, 1, 1, 1, 1
  401.     };
  402.  
  403.     final static public int[] seriesGalleryPage = {
  404.             0, 0, 0, 0, 0, 0, 0, 0, 0,
  405.             3, 0, 0,
  406.             2, 3, 2, 4, 4, 3, 3, 5, 5, 3, 4, 5,
  407.             5, 4, 4, 1, 1, 1, 4, 3, 5, 1, 4, 1,
  408.             1, 1, 0, 4, 2, 3, 4, 3, 3, 5
  409.     };
  410.  
  411.     */
  412.     /*final*/ static public /*Class[]*/ $seriesTypesOf = Array(
  413.                                                 Line,
  414.                                                 Points,
  415.                                                 Area,
  416.                                                 FastLine,
  417.                                                 HorizLine,
  418.                                                 Bar,
  419.                                                 HorizBar,
  420.                                                 Pie,
  421.                                                 Shape,
  422.                                                 Arrow,
  423.                                                 Bubble,
  424.                                                 Gantt,
  425.                                                 Candle,
  426.                                                 Donut,
  427.                                                 Volume,
  428.                                                 Bar3D,
  429.                                                 Points3D,
  430.                                                 Polar,
  431.                                                 Radar,
  432.                                                 Clock,
  433.                                                 WindRose,
  434.                                                 Pyramid,
  435.                                                 Surface,
  436.                                                 LinePoint,
  437.                                                 BarJoin,
  438.                                                 ColorGrid,
  439.                                                 Waterfall,
  440.                                                 Histogram,
  441.                                                 Error,
  442.                                                 ErrorBar,
  443.                                                 Contour,
  444.                                                 Smith,
  445.                                                 Calendar,
  446.                                                 HighLow,
  447.                                                 TriSurface,
  448.                                                 Funnel,
  449.                                                 Box,
  450.                                                 HorizBox,
  451.                                                 HorizArea,
  452.                                                 Tower,
  453.                                                 PointFigure,
  454.                                                 Gauges,
  455.                                                 Vector3D,
  456.                                                 Map,
  457.                                                 Bezier,
  458.                                                 ImageBar
  459.     );
  460.     /*
  461.     public static InputStream downloadURLStream(String url) throws
  462.             MalformedURLException, IOException {
  463.         return new URL(url).openStream();
  464.     }
  465.  
  466.     public static String downloadURLString(String url) throws IOException {
  467.  
  468.         Reader reader = new InputStreamReader(downloadURLStream(url), "UTF-8");
  469.  
  470.         int c;
  471.         StringBuffer sb = new StringBuffer();
  472.         while ((c = reader.read()) != -1) {
  473.             sb.append((char) c);
  474.         }
  475.  
  476.         return sb.toString();
  477.     }
  478.  
  479.  
  480.     static public function MulDiv($a, $b, $c) {
  481.         (int) $nMul = $a * $b,
  482.                    nMod = nMul % c,
  483.                           nRes = nMul / c;
  484.  
  485.         if (nMod >= c / 2) { // Round up if >= 0.5
  486.             nRes++;
  487.         }
  488.         return nRes;
  489.     }
  490.  
  491.     static public String dateTimeToStr(double datetime) {
  492.         return new DateTime(datetime).toShortDateString();
  493.     }
  494.  
  495.     static public String dateTimeToStr(DateTime datetime) {
  496.         return datetime.toShortDateString();
  497.     }
  498.  
  499.     static public String dateTimeToDateTimeStr(DateTime datetime) {
  500.         return datetime.toShortDateString() + " " + datetime.toShortTimeString();
  501.     }
  502.  
  503.  
  504.     static public String timeToStr(double datetime) {
  505.         return new com.steema.teechart.DateTime(datetime).toShortTimeString();
  506.     }
  507.  
  508.     static public String timeToStr(DateTime datetime) {
  509.         return datetime.toShortTimeString();
  510.     }
  511.  
  512.     static public String arrayToString(String[] a) {
  513.         return arrayToString(a, '\n');
  514.     }
  515.  
  516.     static public String arrayToString(String[] a, char separator) {
  517.         StringBuffer result = new StringBuffer();
  518.         if (a.length > 0) {
  519.             result.append(a[0]);
  520.             for (int i = 1; i < a.length; i++) {
  521.                 result.append(separator);
  522.                 result.append(a[i]);
  523.             }
  524.         }
  525.         return result.toString();
  526.     }
  527.  
  528.     static public void drawCheckBox(int x, int y, IGraphics3D g,
  529.                                     boolean drawChecked, boolean CheckBox,
  530.                                     java.awt.Color backColor) {
  531.  
  532.         int TeeCheckBoxSize = 11;
  533.  
  534.         g.getPen().setStyle(DashStyle.SOLID);
  535.         g.getPen().setWidth(1);
  536.         g.getPen().setColor(Color.GRAY);
  537.         g.line(x + TeeCheckBoxSize, y, x, y);
  538.         g.line(x, y, x, y + TeeCheckBoxSize + 1);
  539.  
  540.         g.getPen().setColor(Color.lightGray);
  541.         g.line(x, y + TeeCheckBoxSize + 1, x + TeeCheckBoxSize + 1,
  542.                y + TeeCheckBoxSize + 1);
  543.         g.line(x + TeeCheckBoxSize + 1, y + TeeCheckBoxSize + 1,
  544.                x + TeeCheckBoxSize + 1, y);
  545.  
  546.         g.getPen().setColor(Color.BLACK);
  547.         g.line(x + 1, y + 1, x + TeeCheckBoxSize, y + 1);
  548.         g.line(x + 1, y + 1, x + 1, y + TeeCheckBoxSize);
  549.  
  550.         if (drawChecked) {
  551.             g.getPen().setStyle(DashStyle.SOLID);
  552.             g.getPen().setColor(Color.BLACK);
  553.             for (int t = 1; t < 4; t++) {
  554.                 g.line(x + 2 + t, y + 4 + t, x + 2 + t, y + 7 + t);
  555.             }
  556.             for (int t = 1; t < 5; t++) {
  557.                 g.line(x + 5 + t, y + 7 - t, x + 5 + t, y + 10 - t);
  558.             }
  559.         }
  560.     }  */
  561. }
  562.  
  563. ?>

Documentation generated on Wed, 16 Jun 2010 12:09:06 +0200 by phpDocumentor 1.4.1